﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using PagedList;

namespace DealKhuyenMaiV2.com.Controllers
{
    public class StatisticsController : BaseController
    {
        //
        // GET: /Statistics/

        public ViewResult Index()
        {
            var accounts = DbConText.Accounts.Include("TypeAccount").Include("Ward").Where(a => a.IDTypeAccount == 2);
            int pageSize = 10;
            ViewBag.Methol = "Index";
            return View(accounts.ToList().ToPagedList(1, pageSize));
        }

        [HttpPost]
        public PartialViewResult Index(int? page)
        {
            var accounts = DbConText.Accounts.Include("TypeAccount").Include("Ward").Where(a => a.IDTypeAccount == 2);
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            ViewBag.Methol = "Index";
            return PartialView("_ListCompany", accounts.ToList().ToPagedList(pageNumber, pageSize));
        }

        public ActionResult GetStatistics(int id)
        {
            TongQuan tq = GetStatisticsByID(id);
            return View(tq);
        }

        public TongQuan GetStatisticsByID(int id)
        {
            TongQuan tq = new TongQuan();
            try
            {
                var orders =
                from o in DbConText.Orders
                join p in DbConText.Products on o.IDProduct equals p.IDProduct
                where p.IDAccount == id
                select o;

                decimal TongBanHang = orders.Sum(o => o.Total);

                decimal TongBanHangTrongNam = orders.Where(o => o.DateAdded.Year == DateTime.Now.Year).Sum(o => o.Total);
                int TongDonDatHang = orders.Count();
                int TongSanPham = DbConText.Products.Where(p => p.IDAccount == id).Count();

                tq.TongBanHang = TongBanHang;
                tq.TongBanHangTrongNam = TongBanHangTrongNam;
                tq.TongDonDatHang = TongDonDatHang;
                tq.TongSanPham = TongSanPham;
            }
            catch
            {
            }
            return tq;
        }
        public decimal TongDoanhThuTrongThang()
        {
            DateTime month = DateTime.Now;
            var orders = from p in DbConText.Orders where p.DateAdded.Month == month.Month select p;
            decimal doanhthu = 0;
            if (orders == null)
            {
                return 0;
            }
            foreach (Order order in orders)
            {
                doanhthu += order.Total;
            }
            return doanhthu;

        }

        public ActionResult DoanhThuDoiVoiTungNhaCungCap()
        {
            //get list doanh nghiep
            var id = from a in DbConText.Products select a.IDAccount;
            id = id.Distinct();
            List<Decimal> doanhthu = new List<decimal>();
            foreach (int i in id)
            {
                decimal dt = TinhDoanhTheoAccount(i);
                doanhthu.Add(dt);
            }

            ViewBag.ListID = id.ToList();
            ViewBag.ListDT = doanhthu.ToList();

            return View();
        }

        public decimal TinhDoanhTheoAccount(int id)
        {
            //lay ra cac san pham cua account do
            var products = from p in DbConText.Products where p.IDAccount == id select p;
            //tinh cac order của san pham do
            List<Order> orders = new List<Order>();
            foreach (Product pro in products)
            {
                var _orders = from p in DbConText.Orders where p.IDProduct == pro.IDProduct select p;
                foreach (Order o in _orders)
                {
                    orders.Add(o);
                }
            }

            decimal doanhthu = 0;
            if (orders == null)
            {
                return 0;
            }
            foreach (Order order in orders)
            {
                doanhthu += order.Total;
            }
            return doanhthu;

        }

    }
}
